﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using NLog;

namespace com.dz.ct.common
{

         /* ***********************************************
   * 功能描述: 
   * 1、文件管理类
   * 2、写文件内容、读文件内容、主要用于章节内容信息存放
   * ***********************************************/
    public class FileManager
    {

        static object m_obj = new object();
   
        /// <summary>
        /// 读取书的章节信息
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_chapter_id"></param>
        /// <returns></returns>
        public string GetBookContent(long i_book_id, long i_chapter_id)
        {
            string t_path = AppConfig.BookChapterRoot;
            string t_file_name = Path.Combine(t_path, i_book_id.ToString());
            t_file_name = Path.Combine(t_file_name, string.Format("{0}.txt", i_chapter_id));
            if (!File.Exists(t_file_name))
            {

                LogHelper.Error("读取文件" + t_file_name + "失败,文件不存在!");
                return string.Empty;
            }

            //逐行读取文本内容
            string t_text;

            try
            {
                using (StreamReader t_sm_read = new StreamReader(t_file_name, System.Text.Encoding.Default))
                {
                    t_text = t_sm_read.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("读取文件出现异常" + ex.ToString());
                return string.Empty;
            }
            return t_text;
        }

        /// <summary>
        /// 章节写文件
        /// </summary>
        /// <param name="i_site"></param>
        /// <param name="i_chapter"></param>
        /// <returns></returns>
        public bool WriteBookChapter(string i_site, BookChapterInfo i_chapter)
        {
            lock (m_obj)
            {
                lock (m_obj)
                {
                    string t_path = AppConfig.BookChapterRoot;
                    string t_file_name = string.Format("tb{0}-{1}-data.txt", i_site,DateTime.Now.ToString("yyyyMMddHH"));
                    if (!Directory.Exists(t_path))
                    {
                        Directory.CreateDirectory(t_path);
                    }

                    t_file_name = Path.Combine(t_path, t_file_name);
                    try
                    {
                        using (FileStream t_file_stream = new FileStream(t_file_name, FileMode.Append, FileAccess.Write))
                        {
                            using (StreamWriter t_writer = new StreamWriter(t_file_stream, Encoding.Default))
                            {
                                t_writer.Write(i_chapter.ToString());
                                t_writer.Flush();
                                t_writer.Close();
                            }
                        }
                        return true;
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error("写文件信息失败" + ex.ToString());
                        return false;
                    }
                }
            }
        }

        /// <summary>
        /// 写执行sql脚本
        /// </summary>
        /// <param name="i_site"></param>
        /// <param name="i_str_sql"></param>
        /// <returns></returns>
        public bool WriteBookChapterSQL(string i_site,string i_str_sql)
        {
            lock (m_obj)
            {
                string t_path = AppConfig.BookChapterRoot;
                string t_file_name = string.Format("tb{0}-{1}-sql.txt", i_site,DateTime.Now.ToString("yyyyMMddHH"));
                if (!Directory.Exists(t_path))
                {
                    Directory.CreateDirectory(t_path);
                }

                t_file_name = Path.Combine(t_path, t_file_name);
                try
                {
                    using (FileStream t_file_stream = new FileStream(t_file_name, FileMode.Append, FileAccess.Write))
                    {
                        using (StreamWriter t_writer = new StreamWriter(t_file_stream, Encoding.Unicode))
                        {
                            t_writer.WriteLine(i_str_sql);
                            t_writer.Flush();
                            t_writer.Close();
                        }
                    }

                    return true;
                }
                catch (Exception ex)
                {
                    LogHelper.Error("写文件信息失败" + ex.ToString());
                    return false;
                }
            }
        }

        /// <summary>
        /// 写书的章节文本
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_chapter_id"></param>
        /// <param name="i_text"></param>
        /// <returns></returns>
        public int WriteBookContent(string i_book_category_id, long i_book_id, long i_chapter_id, string i_text)
        {
            lock (m_obj)
            {
                string t_path = AppConfig.BookChapterRoot;
                t_path = Path.Combine(t_path, i_book_category_id);
                t_path = Path.Combine(t_path, i_book_id.ToString());
                if (!Directory.Exists(t_path))
                {
                    Directory.CreateDirectory(t_path);
                }

                string t_file_name = Path.Combine(t_path, string.Format("{0}.txt", i_chapter_id));
                try
                {
                    using (FileStream t_file_stream = new FileStream(t_file_name, FileMode.Append, FileAccess.Write))
                    {
                        using (StreamWriter t_writer = new StreamWriter(t_file_stream, Encoding.Unicode))
                        {
                            t_writer.WriteLine(i_text);
                            t_writer.Flush();
                            t_writer.Close();
                        }
                    }

                    return Consts.Success;
                }
                catch (Exception ex)
                {
                    LogHelper.Error("写文件信息失败" + ex.ToString());
                    return Consts.Faild;
                }
            }
        }

        /// <summary>
        /// 检查章节内容是否已存在
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_chapter_id"></param>
        /// <returns></returns>
        public bool CheckChaperExist(long i_book_id, long i_chapter_id)
        {
            try
            {
                string t_path = AppConfig.BookChapterRoot;
                t_path = Path.Combine(t_path, i_book_id.ToString());
                if (!Directory.Exists(t_path))
                {
                    Directory.CreateDirectory(t_path);
                }
                string t_file_name = Path.Combine(t_path, string.Format("{0}.txt", i_chapter_id));

                return File.Exists(t_file_name);
            }
            catch (Exception ex)
            {
                LogHelper.Error("检查章节内容是否存在失败" + ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// 创建/读取检查文件
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        private FileInfo CreatFile(string i_fileName)
        {
            FileInfo t_checkedFile = null;
            try
            {
                t_checkedFile = new FileInfo(i_fileName);
           
            }
            catch (Exception ex)
            {
                LogHelper.Error("创建读取检查文件失败" + ex.ToString());
            }

            return t_checkedFile;
        }


     

     
    }
}
